<template>
    <div>
        业务类型：
        <el-select v-model="path" placeholder="应付转应付" style="width: 120px;" @change="goto">
            <el-option value="WriteOff">
                预收冲应收
            </el-option>
            <el-option value="WriteOff2">
                预付冲应付
            </el-option>
            <el-option value="WriteOff3">
                应收冲应付
            </el-option>
            <el-option value="WriteOff4">
                应收转应收
            </el-option>
            <el-option value="WriteOff5">
                应付转应付
            </el-option>
        </el-select>
        转出供应商：
        <el-input style="width: 150px;margin-left: 10px;margin-right: 20px;" placeholder="请输入客户编号"
            v-model="receipt.providerName1" clearable>
        </el-input>
        <el-button slot="reference" style="margin-left:-20px;margin-right:10px"
            @click="dialogShow1 = true">...</el-button>
        <el-dialog top="20px" title="选择供应商" :visible.sync="dialogShow1">

            <ProviderChoose ref="choosePro" :dialogShow="dialogShow" />
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogShow1 = false">取 消</el-button>
                <el-button type="primary" @click="yes1()">确 定</el-button>
            </div>
        </el-dialog>
        转入供应商：
        <el-input style="width: 150px;margin-left: 10px;margin-right: 20px;" placeholder="请输入客户编号"
            v-model="receipt.providerName2" clearable>
        </el-input>
        <el-button slot="reference" style="margin-left:-20px;margin-right:10px"
            @click="dialogShow = true">...</el-button>
        <el-dialog top="20px" title="选择供应商" :visible.sync="dialogShow">

            <ProviderChoose ref="choosePro" :dialogShow="dialogShow" />
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogShow = false">取 消</el-button>
                <el-button type="primary" @click="yes">确 定</el-button>
            </div>


        </el-dialog>
        <span class="demonstration" style="margin-left: 20px;">单据日期:</span>
        <el-date-picker v-model="selectedDate" align="right" type="date" placeholder="选择日期"
            :picker-options="pickerOptions" @change="handleDateChange" style="margin-left: 10px;"></el-date-picker>


        <hr style="margin-top: 10px">
        <div style="margin-top: 10px;float: right">
            <el-button @click="add()">保存</el-button><el-button type="primary" @click="git()">提交</el-button>
        </div>
        <div style="clear: both;"></div>
        <div style="clear: both"></div>
        <div style="float:right;margin-top: 10px;margin-left: 200px"> <el-button
                @click="chooseInitForm">选择应付单据</el-button>
        </div>
        <div style="clear: both;"></div>

        <el-dialog top="20px" title="" :visible.sync="choose">
            <ShouldPayForm ref="shouldPayForm" :providerNum="providerId1" :choose="choose" />
            <div slot="footer" class="dialog-footer">
                <el-button @click="choose = false">取 消</el-button>
                <el-button type="primary" @click="yesForm()">确 定</el-button>
            </div>
        </el-dialog>
        <el-table height="180px" :data="formDate" border style="width: 100%; font-size: 13px">
            <el-table-column fixed prop="" label="" width="50">
                <i class="el-icon-circle-plus-outline" style="margin-left: 8px;" @click="addRow1()"></i>
                <br>
                <i class="el-icon-remove-outline" style="margin-left: 8px;"></i>
            </el-table-column>
            <el-table-column prop="documentNum" label="源单编号" width="150">
            </el-table-column>
            <el-table-column prop="saleType" label="业务类别" width="120">
            </el-table-column>
            <el-table-column prop="documentDate" label="单据日期" width="120">
            </el-table-column>
            <el-table-column prop="received" label="单据金额" width="120">
            </el-table-column>
            <el-table-column prop="writtenOffMoney" label="已核销金额" width="120">
            </el-table-column>
            <el-table-column prop="noMoney" label="未核销金额" width="120">
            </el-table-column>
            <el-table-column prop="nowMoney" label="本次核销金额" width="130">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.nowMoney" style="width: 100px;margin-left: 10px;margin-right: 20px;"
                        placeholder="收款金额" clearable>
                    </el-input>
                </template>
            </el-table-column>
            <el-table-column prop="zip" label="单据备注" width="120">
            </el-table-column>
        </el-table>

        <el-input type="textarea" :rows="2" placeholder="暂无备注信息" v-model="receipt.remark"
            style="width: 100%;line-height: 20px;">

        </el-input>
        <div style="float:right;margin-right: 20px;margin-top: 10px;">
            <el-button @click="goto('WriteOffHistory')">历史单据</el-button><el-button>操作日志</el-button>
        </div>

    </div>

</template>

<script>
import ProviderChoose from '../public_com/ProviderChoose.vue';
import ShouldPayForm from './ShouldPayForm.vue';
import Top from './Top.vue';

export default {
    name: "WriteOff",
    components:
    {
        Top,
        ProviderChoose,
        ShouldPayForm
    },
    data() {
        return {
            dialogShow1: false,
            dialogShow: false,
            id: "",
            yList: "",
            selectedDate: "",
            dialogFormVisible1: false,
            dialogFormVisible: false,
            choose: false,
            choose1: false,
            path: "",
            providerId1: "",
            providerName1: "",
            providerType1: "",
            providerId2: "",
            providerName2: "",
            providerType2: "",
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            receipt: {
                id: "",
                consumerCode1: "",
                consumerCode2: "",
                providerCode1: "",
                providerCode2: "",
                doId: 1,
                id: "",
                writeOffMoney: "",
                orderDate: "",
                serviceType: "应付转应付",
                remark: "",
            },
            formDate: [
                {
                    documentNum: "",
                    documentDate: "",
                    received: "",
                    writtenOffMoney: 0,
                    remark: "",
                    noMoney: 0,
                    nowMoney: 0,
                    saleType: ""
                },
            ],
            value1: '',
            value2: '',
        };
    },
    methods: {
        git: function () {
            let formMoney = 0
            // let writeMoney = 0
            for (var i = 0; i < this.formDate.length; i++) {
                if (this.formDate[i].nowMoney > this.formDate[i].noMoney) {
                    alert("本次核销金额不能大于未核销金额")
                    return
                }
            }
            this.formDate.forEach(item => {

                const writtenOffMoney = parseFloat(item.nowMoney) || 0;
                formMoney += writtenOffMoney
            })
            this.receipt.writeOffMoney = formMoney
            // console.log(this.receipt.writeoffMoney)
            // console.log(this.receipt.preMoney)
            // return
            const reg = /^([1-9]\d*(\.\d{1,2})?|0\.\d{1,2})$/
            console.log(this.receipt.customerCode1)
            if (this.receipt.customerCode1 === "") {
                alert("请选择转出供应商")
                return
            }
            if (this.receipt.customerCode2 === "") {
                alert("请选择转入供应商")
                return
            }
            if (this.receipt.orderDate === "") {
                alert("请选择日期")
                return
            }
            for (const contact of this.formDate) {
                if (contact.nowMoney === "") {
                    alert("请输入本次核销金额")
                    return
                }
                if (!reg.test(contact.nowMoney)) {
                    alert("本次核销金额不符合规范")
                    return
                }
                if (isNaN(contact.nowMoney)) {
                    alert("收款金额必须为数字")
                    return
                }
            }
            console.log(this.tableData)
            console.log(this.receipt)
            // alert(this.receipt.writeoffMoney)
            // return
            const formData = new FormData();
            formData.append('shouldForm', JSON.stringify(this.formDate))
            formData.append('writeOffForm', JSON.stringify(this.receipt))
            formData.append('git', '1')
            this.axios.post(
                "/cash/writeOff/insert",
                formData,
                {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }
            ).then(rs => {
                console.log(rs)
                if (rs.data.code === 200) {
                    alert("保存成功")
                    window.location.reload
                } else {
                    alert("保存失败")
                }
            })
        },
        chooseInitForm: function () {
            if (this.receipt.providerCode1 === "") {
                alert("请选择转出供应商")
            } else {
                this.choose = true
            }
        },
        add: function () {
            let formMoney = 0
            // let writeMoney = 0
            for (var i = 0; i < this.formDate.length; i++) {
                if (this.formDate[i].nowMoney > this.formDate[i].noMoney) {
                    alert("本次核销金额不能大于未核销金额")
                    return
                }
            }
            this.formDate.forEach(item => {

                const writtenOffMoney = parseFloat(item.nowMoney) || 0;
                formMoney += writtenOffMoney
            })
            this.receipt.writeOffMoney = formMoney
            // console.log(this.receipt.writeoffMoney)
            // console.log(this.receipt.preMoney)
            // return
            const reg = /^([1-9]\d*(\.\d{1,2})?|0\.\d{1,2})$/
            console.log(this.receipt.customerCode1)
            if (this.receipt.customerCode1 === "") {
                alert("请选择转出供应商")
                return
            }
            if (this.receipt.customerCode2 === "") {
                alert("请选择转入供应商")
                return
            }
            if (this.receipt.orderDate === "") {
                alert("请选择日期")
                return
            }
            for (const contact of this.formDate) {
                if (contact.nowMoney === "") {
                    alert("请输入本次核销金额")
                    return
                }
                if (!reg.test(contact.nowMoney)) {
                    alert("本次核销金额不符合规范")
                    return
                }
                if (isNaN(contact.nowMoney)) {
                    alert("收款金额必须为数字")
                    return
                }
            }
            console.log(this.tableData)
            console.log(this.receipt)
            // alert(this.receipt.writeoffMoney)
            // return
            const formData = new FormData();
            formData.append('shouldForm', JSON.stringify(this.formDate))
            formData.append('writeOffForm', JSON.stringify(this.receipt))
            this.axios.post(
                "/cash/writeOff/insert",
                formData,
                {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }
            ).then(rs => {
                console.log(rs)
                if (rs.data.code === 200) {
                    alert("保存成功")
                    window.location.reload
                } else {
                    alert("保存失败")
                }
            })
        },
        handleDateChange(value) {
            // value 即为选中的日期值
            console.log('选中的日期是：', value);
            this.receipt.orderDate = value
            // 在这里可以执行更多的逻辑，比如更新数据、调用 API 等
        },
        yesForm() {
            this.choose = false
            this.formDate = this.$refs.shouldPayForm.selectionList
            console.log(this.$refs.shouldPayForm.selectionList)
        },
        yes1() {
            this.dialogShow1 = false
            var list = this.$refs.choosePro.selectionList
            this.receipt.providerName1 = list[0].providerName
            this.receipt.providerCode1 = list[0].providerCode
            this.providerId1 = list[0].providerId
            console.log(this.providerId1)
        },
        yes() {
            this.dialogShow = false
            var list = this.$refs.choosePro.selectionList
            this.receipt.providerName2 = list[0].providerName
            this.receipt.providerCode2 = list[0].providerCode

        },
        goto() {
            this.$router.push(this.path)
        },
        goto(to) {
            this.$router.push(to)
        }
    }

}
</script>

<style></style>